package View;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.ScrollPaneConstants;
import javax.swing.SpinnerNumberModel;

import Controller.DB;
import Controller.RUWChangeListener;
import Model.ClickableLabel;
import Model.Customer;
import Model.MenuItem;
import Model.RUWSpinner;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;

@SuppressWarnings("serial")
public class MenuPanel extends JPanel {
	private RestUWantGUI myGUI;
	private DB myDatabase;

	/**
	 * Create the panel.
	 * @throws SQLException 
	 */
	public MenuPanel(RestUWantGUI GUI, final DB database, final Customer customer) throws SQLException {
		myGUI = GUI;
		myDatabase = database;
		
		
		String[] columnNames = {"Name",
								"Description",
								"Price",
								"Quantity"
		};
		
		List<MenuItem> itemData = new ArrayList<MenuItem>();
		itemData = myDatabase.getMenuItems();
		
		JPanel itemPanel = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		
		itemPanel.setLayout(gridBagLayout);		
		
		GridBagConstraints gc = new GridBagConstraints();

		gc.gridx = 0;
		gc.gridy = 0;
		for(String s : columnNames) {
			gc.gridx++;
			gc.ipady = 5;
			gc.ipadx = 25;
			itemPanel.add(new JLabel(s), gc);
		}
		gc.gridy = 1;
		for (int i = 0; i < itemData.size(); i++) {
			gc.fill = GridBagConstraints.NONE;
			gc.gridx = 0;
			gc.ipady = 5;
			gc.ipadx = 25;
			final MenuItem item = itemData.get(i);
			gc.gridx++;
			ClickableLabel name = new ClickableLabel(item.getName());
			name.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					 new ItemDetailPopUp(customer,item);
				}
			});
			itemPanel.add(name, gc);
			gc.gridx++;
			String truncateDesc = item.getDescription();
			if (truncateDesc.length() > 20) {
				truncateDesc = truncateDesc.substring(0, 19);
				truncateDesc += "...";
			}
			itemPanel.add(new JLabel(truncateDesc), gc);
			gc.gridx++;			
			itemPanel.add(new JLabel(String.format("%4.2f", item.getPrice())), gc);
			gc.gridx++;
			gc.fill = GridBagConstraints.HORIZONTAL;
			JSpinner spinner = new RUWSpinner(new SpinnerNumberModel(0, 0, 10, 1), item, customer);
			spinner.addChangeListener(new RUWChangeListener());
			itemPanel.add(spinner, gc);
			gc.gridy++;
		}
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane(itemPanel, 
												 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
												 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		scrollPane.setPreferredSize(new Dimension(myGUI.getWidth() - 50, myGUI.getHeight() - 165));
		add(scrollPane);
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);
		
		JButton submitButton = new JButton("Submit Order");
		panel.add(submitButton);
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(customer.getOrder().hasItems()) {
					database.addOrder(customer);
					myGUI.orderToComplete();
				}
			}
		});

	}
}
